package com.wwj.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xya on 16-10-27.
 */
public class ResourceUtil {
    /**
     * 将时间解析成字符串
     *
     * @param timeRule 用当时时间替换包含.{year}/{month}/{day}/{time}/{min}/{sec}的数据
     * @return
     */
    public static String parseTimeRuleStr(String timeRule, Date date) {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
        c.setTime(date);
        timeRule = timeRule.replaceAll("\\{year\\}", String.valueOf(c.get(Calendar.YEAR)));
        timeRule = timeRule.replaceAll("\\{month\\}", c.get(Calendar.MONTH) + 1 > 9 ? String.valueOf(c.get(Calendar.MONTH) + 1) : "0" + String.valueOf(c.get(Calendar.MONTH) + 1));
        timeRule = timeRule.replaceAll("\\{day\\}", c.get(Calendar.DAY_OF_MONTH) > 9 ? String.valueOf(c.get(Calendar.DAY_OF_MONTH)) : "0" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)));
        timeRule = timeRule.replaceAll("\\{time\\}", c.get(Calendar.HOUR_OF_DAY) > 9 ? String.valueOf(c.get(Calendar.HOUR_OF_DAY)) : "0" + String.valueOf(c.get(Calendar.HOUR_OF_DAY)));
        timeRule = timeRule.replaceAll("\\{min\\}", c.get(Calendar.MINUTE) > 9 ? String.valueOf(c.get(Calendar.MINUTE)) : "0" + String.valueOf(c.get(Calendar.MINUTE)));
        timeRule = timeRule.replaceAll("\\{sec\\}", c.get(Calendar.SECOND) > 9 ? String.valueOf(c.get(Calendar.SECOND)) : "0" + String.valueOf(c.get(Calendar.SECOND)));
        return timeRule;
    }
}
